package com.xinzhi.service.Impl;

import com.xinzhi.dao.IUserDao;
import com.xinzhi.entity.User;
import com.xinzhi.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.UUID;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserDao iud;
    @Override    //登录
    public User login(String phone, String password) {
        User user = new User();
        user.setPhone(phone);
        user.setPassword(password);
        User ui = iud.selectUser(user);
        if (ui != null){
            if(ui.getPassword().equals(password)){

                return ui;
            }
        }
        return null;
    }



    @Override  //注册
    public String register(String phone, String password) {
        User user = new User();
        user.setPhone(phone);
        user.setPassword(password);
        user.setStatus(1);
        user.setUsername(generateRandomUsername());
        user.setCreated_at(new java.sql.Date(System.currentTimeMillis()));
        int register = iud.insertUser(user);
        return register > 0 ? "注册成功" : "注册失败";
    }
    private String generateRandomUsername() {
        // 使用UUID生成一个随机的用户名
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    @Override
    public boolean phoneNumber(String phone) {
        String regex ="^1[3456789]\\d{9}$";
        return phone.matches(regex);
        //验证手机号
    }
}
